home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13380 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: [QE] : file open problem
  5. Date: 7 Apr 96 18:54:20 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.828903260@rscernix>
  8. References: <4k8mja$ddn@news.kreonet.re.kr>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <4k8mja$ddn@news.kreonet.re.kr> Parao@kyebek.kjist.ac.kr (Don Kim) writes:
  13.  
  14. >Hello, there
  15. >
  16. >I have a problem with file opening.
  17. >In my code,
  18. >
  19. >    saveTextToFile ( char *text, char *filename)    {
  20. >    FILE *file;
  21. >
  22. >    if ( ( file = fopen (filename, "w") = = NULL ) 
  23.                                             ^^^
  24. 1.                               This is a syntax error in C.
  25.  
  26. 2. The parentheses don't match.
  27.  
  28. >        cout << "Cannot open file" << endl;
  29.  
  30. This line is completely meaningless in C.  The << operator doesn't
  31. accept pointer operands (and it's not clear from your code what is the
  32. type of cout and endl :-)
  33.  
  34. >    else    {
  35. >        (void) fprintf (file, "%s\n", text);
  36. >        (void) fclose(file);
  37. >    }
  38. >   }
  39. >
  40. >   :
  41. >   :
  42. >
  43. >   char *_type = "part";    
  44. >   saveTextToFile ( _type, _type );
  45. >
  46. >Is anything wrong in my source code ? My program always displays "Cannot 
  47. >open file"....
  48. >
  49. >Let me know how to fix the problem.
  50.  
  51. 1. Write it in C (or post to c.l.c++ if you insist on using C++).
  52.  
  53. 2. The line opening the file should look like this:
  54.  
  55.     if ((file = fopen(filename, "w")) == NULL)
  56.  
  57. 3. To be on the safe side, avoid identifiers starting with an underscore.
  58.    Sometimes they're legal, some other times they are in the implementation
  59.    namespace and it isn't worth the effort to try to figure out whether
  60.    they can be used in a certain context or not.  They only create
  61.    headaches without buying you anything at all.
  62.  
  63. Dan
  64. --
  65. Dan Pop
  66. CERN, CN Division
  67. Email: danpop@mail.cern.ch 
  68. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  69.